﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using Husb.Framework;
using Crawler.Model;
using Crawler.BusinessActions;
using Husb.Util;
using System.Text.RegularExpressions;


namespace WinFromsClient
{
    public partial class CaptureRuleForm : Form
    {
        //private TaskItemInfo taskItem;
        private CaptureRuleInfo rule;
        public CaptureRuleForm(CaptureRuleInfo cr)
        {
            InitializeComponent();

            this.captureRuleInfoBindingSource.DataSource = cr;
            this.rule = cr;

            WinUtil.PopulateListBox(categoryComboBox, ProjectConstants.CaptureCategory, false);
            WinUtil.PopulateListBox(contentTypeComboBox, ProjectConstants.ContentType, false);
            WinUtil.PopulateListBox(processMethodComboBox, ProjectConstants.ProcessMethod, false);
            WinUtil.PopulateListBox(matchMethodComboBox, ProjectConstants.MatchMethod, false);

            this.categoryComboBox.DataBindings.Add(new System.Windows.Forms.Binding("SelectedValue", this.captureRuleInfoBindingSource, "Category", true));
            this.contentTypeComboBox.DataBindings.Add(new System.Windows.Forms.Binding("SelectedValue", this.captureRuleInfoBindingSource, "ContentType", true));
            this.processMethodComboBox.DataBindings.Add(new System.Windows.Forms.Binding("SelectedValue", this.captureRuleInfoBindingSource, "ProcessMethod", true));
            this.matchMethodComboBox.DataBindings.Add(new System.Windows.Forms.Binding("SelectedValue", this.captureRuleInfoBindingSource, "MatchMethod", true));
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            object o = categoryComboBox.SelectedValue;

            this.captureRuleInfoBindingSource.EndEdit();
            //CaptureRuleInfo cr = this.captureRuleInfoBindingSource.DataSource as CaptureRuleInfo;
            //CaptureRule.Update(
        }

        private void btnTest_Click(object sender, EventArgs e)
        {
            //rule.TaskItem.Url = "http://www.cnblogs.com/frank_xl/archive/2010/01/20/1652769.html";

            string html = Crawler.BusinessActions.Utilities.GetPageHTML(rule);
            //MatchCollection matches = Utilities.GetMatches(rule.StartString, rule.EndString, html);

            TestForm form = new TestForm();
            form.LoadHtml(html);
            form.ShowDialog();

        }

        private void btnTestNavi_Click(object sender, EventArgs e)
        {
            TestContentPageForm form = new TestContentPageForm(rule);
            form.ShowDialog();
            
        }

        private static MatchCollection GetMatches(string pattern, string originalText)
        {
            Regex tmpRegex = new Regex(pattern, RegexOptions.IgnoreCase);
            MatchCollection matches = tmpRegex.Matches(originalText);

            return matches;
        }
    }
}
